<template>
<div class="detail">
  <div class="container" :style="{width: width ? width : '1100px'}">
    <div class="content" v-if="Object.keys(playListDetail).length">
      <div class="header">
        <div class="avatarImg">
          <img :src="playListDetail.coverImgUrl" alt="playListDetail.name">
        </div>
        <div class="playlistInfo">
          <div class="name">
            <span>歌单 </span>
            <span> {{playListDetail.name}}</span>
          </div>
          <div class="author">
            <img :src="creator.avatarUrl" :alt="creator.nickname">
            <span>{{creator.nickname}}</span>
            <span>{{createTime}} 创建</span>
          </div>
          <div class="tags">
            <p><strong>标签：</strong><span v-for="(tag, index) in tags" :key="index">{{tag}}</span></p>
            <p><strong>歌曲：</strong>{{playListDetail.trackCount}} 播放：{{playListDetail.playCount}}次</p>
            <p><strong>介绍：</strong>{{playListDetail.description}}</p>
          </div>
        </div>
      </div>
      <div class="playlist">
        <div class="title">
          <span>歌曲列表</span>
          <span>{{playListDetail.trackCount}}首</span>
          <span>播放：<strong class="playCount">{{playListDetail.playCount}}</strong>次</span>
        </div>
        <table>
          <thead>
            <th></th>
            <th></th>
            <th>歌曲标题</th>
            <th>歌手</th>
            <th>专辑</th>
          </thead>
          <tbody>
            <tr v-for="(item, index) in playlist" :key="item.id">
              <td>{{index + 1}}</td>
              <td><a href="javascript:;"><i class="iconfont icon-bofang1"></i></a></td>
              <td><a href="javascript:;" @click="handleToPlayMusic(item.id)">{{item.name}}</a></td>
              <td class="ar"><a href="javascript:;" v-for="ar in item.ar" :key="ar.id">{{ar.name}}</a></td>
              <td><a href="javascript:;">{{item.name}}</a></td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>
</template>

<script>
import moment from 'moment'
export default {
  name: 'Detail',
  props: ['id', 'width'],
  data () {
    return {
      playListDetail: {}
    }
  },
  watch: {
    // 侦听id属性，当id属性变化的时候，就重新执行请求
    id () {
      this.getPlaylistDetail(this.id)
    }
  },
  computed: {
    playlist () {
      return this.playListDetail.tracks
    },
    creator () {
      return this.playListDetail.creator
    },
    tags () {
      return this.playListDetail.tags
    },
    createTime () {
      return moment(this.playListDetail.createTime).format('YYYY-MM-DD')
    }
  },
  methods: {
    async getPlaylistDetail (id) {
      const res = await this.$http.get(`/playlist/detail?id=${id}`, { withCredentials: true })
      // console.log(res.data.playlist)
      this.playListDetail = res.data.playlist
    },
    handleToPlayMusic (id) {
      this.$router.push(`/music/${id}`)
    }
  },
  created () {
    this.getPlaylistDetail(this.id)
  }
}
</script>

<style lang="less" scoped>
.detail{
  margin-top: 5px;
}

.container{
  // width: 1100px;
  margin: 0 auto;
  background-color: #fff;
  overflow: hidden;
  border: 1px solid #ddd;
}

.content{
  width: 100%;
  border-right: 1px solid #ddd;
}

.header{
  padding: 20px;
  overflow: hidden;
  width: 100%;
  .avatarImg{
    float: left;
    width: 25%;
    border: 1px solid #ddd;
    padding: 5px;
    background-color: #eee;
    img{
      display: block;
      width: 100%;
    }
  }
  .playlistInfo{
    margin-left: 50px;
    float: left;
    width: 65%;
    // background-color: aqua;
    font-size: 16px;
    .name{
      margin: 10px 0;
      span{
        margin-right: 10px;
        font-weight: 600;
      }
      span:first-child{
        color: #e60026;
        border: 2px solid #e60026;
        padding: 0 5px 0 8px;
        border-radius: 10px;
      }
    }
    .author{
      margin: 20px 0;
      img{
        width: 40px;
        height: 40px;
        vertical-align: middle;
        border-radius: 20px;
      }
      span{
        margin: 0 10px;
      }
    }
    .tags{
      p{
        margin: 10px 0;
      }
      p:first-child{
        span:not(:last-child)::after{
          content: '/';
          margin: 0 5px;
        }
      }
    }
  }
}

.playlist{
  padding: 20px;
  .title{
    padding: 10px 0;
    border-bottom: 2px solid #c20c0c;
    span:first-child{
      font-size: 20px;
      margin-right: 20px;
    }
    span{
      font-size: 12px;

    }
    span:last-child{
      float: right;
      font-size: 16px;
      strong{
        color: #c20c0c;
      }
    }
  }
  table{
    width: 100%;
    text-align: left;
    font-size: 14px;
    border: 1px solid #ddd;
    // 取消table表格间隔
    border-collapse:collapse;
    thead{
      th:not(:last-child, :first-child){
        border-right: 1px solid #ddd;
      }
      th{
        padding: 10px;
        border-bottom: 1px solid #ddd;
      }
    }
    tbody{
      border-top: 1px solid #ddd;
      tr{
        td{
          padding: 10px;
          max-width: 150px;
          &:first-child, &:nth-child(2){
            width: 20px;
          }
          &:nth-child(2) a:hover{
            color: #666;
            font-weight: 600;
          }
          a{
            color: #333333;
          }
          a:not(&:nth-child(2) a):hover{
            text-decoration: underline;
          }
        }
        .ar{
          a:not(a:last-child)::after{
            content: '/';
            margin: 0 5px;
          }
        }
      }
      // odd: 奇数  even：偶数
      tr:nth-child(even){
        background-color: #eee;
      }
    }
  }
}
</style>
